class Solution {
    int ret, m, n, cnt, dx[4] = { 0, 0, 1, -1 }, dy[4] = { 1, -1, 0, 0 };
    bool visited[101][101];
    int digit(int x)
    {
        int sum = 0;
        while(x)
        {
            sum += (x % 10);
            x /= 10;
        }
        return sum;
    }

    void dfs(int i, int j)
    {
        if(digit(i) + digit(j) > cnt)
            return;

        ret++;
        visited[i][j] = true;

        for(int k = 0; k < 4; k++)
        {
            int x = i + dx[k], y = j + dy[k];
            if(x >= 0 && x < m && y >= 0 && y < n && visited[x][y] == false)
                dfs(x, y);
        }        
    }
public:
    int wardrobeFinishing(int _m, int _n, int _cnt) {
        m = _m, n = _n, cnt = _cnt, ret = 0;
        dfs(0, 0);
        return ret;
    }
};